package cn.yunyichina.provider.hymed.service.impl;

import cn.yunyichina.provider.framework.annotation.validate.ValidateException;
import cn.yunyichina.provider.framework.annotation.validate.Validator;
import cn.yunyichina.provider.framework.common.PKGenerator;
import cn.yunyichina.provider.framework.common.ResponseEnum;
import cn.yunyichina.provider.framework.dubbo.service.BaseService;
import cn.yunyichina.provider.framework.dubbo.service.CacheService;
import cn.yunyichina.provider.framework.dubbo.service.PayService;
import cn.yunyichina.provider.hymed.constant.NeusoftFunctionId;
import cn.yunyichina.provider.hymed.constant.RealAuthTypeEnum;
import cn.yunyichina.provider.hymed.model.bo.*;
import cn.yunyichina.provider.hymed.model.domain.HymedPaymentSettingDO;
import cn.yunyichina.provider.hymed.model.vo.*;
import cn.yunyichina.provider.hymed.service.UserService;
import cn.yunyichina.provider.hymed.service.neusoft.NeusoftBaseService;
import cn.yunyichina.provider.hymed.service.neusoft.NeusoftService;
import cn.yunyichina.provider.hymed.service.neusoft.NeusoftServiceImpl;
import cn.yunyichina.provider.iface.entity.base.PersonalUserFamilyDto;
import cn.yunyichina.provider.iface.entity.cache.SessionInfo;
import cn.yunyichina.provider.iface.entity.common.ResCommon;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.provider.iface.entity.common.ResponseObject;
import cn.yunyichina.provider.iface.entity.pay.PaymentSettingResponse;
import cn.yunyichina.utils.commons.IdCardUtils;
import cn.yunyichina.utils.convert.JsonUtils;
import com.qiniu.util.Json;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Lullaby on 2017/10/19
 */
@Service
public class UserServiceImpl extends NeusoftBaseService implements UserService {

    private Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    private final NeusoftService neusoftService;

    @Autowired
    public UserServiceImpl(NeusoftService neusoftService) {
        this.neusoftService = neusoftService;
    }

    /**
     * 一卡通用户信息获取
     * yyt.hymed.user.info.get
     * @param params
     * @return
     */
    @Override
    public ResponseObject getUserInfo(String params) {
        ResponseObject response = new ResponseObject();

        UserInfoRequestVO requestVO = JsonUtils.parseObject(params, UserInfoRequestVO.class);
        assert requestVO != null;

//        PersonalUserFamilyDto familyDto = BaseService.getBaseClient().getPersonalUserFamily(requestVO.getFamilyId());
//        if(familyDto == null){
//            response.setResultCodeAndMessage("24002", "家人数据不存在！");
//            return response;
//        }

        //调用 4.1.2 用户信息获取
        UserInfoRequestBO requestBO = new UserInfoRequestBO();
        requestBO.setPortalLoginId(requestVO.getLoginName());
        requestBO.setFunction_id(NeusoftFunctionId.GET_USER_INFO);
        HymedPaymentSettingDO hymedPaymentSettingDO=getNeusoftSessionId(requestVO);
        requestBO.setSession_id(hymedPaymentSettingDO.getSessionId());
        requestBO.setPrivateKey(hymedPaymentSettingDO.getPrivateKey());
        requestBO.setPublicKey(hymedPaymentSettingDO.getPublicKey());

        UserInfoResponseBO responseBO = neusoftService.doExecute(requestBO, UserInfoResponseBO.class);

        if (responseBO == null || !responseBO.getBizSuccess()) {
            return response.buildResultCodeAndMessageAndResult(
                    ResponseEnum.Common.FAILURE.getCode(),
                    ResponseEnum.Common.FAILURE.getMessage(),
                    responseBO);
        }
        int realNameFlag = 0;//是否实名认证类型
        int bindInsuranceCardFlag = 0;//是否已经绑卡
        int bindMobileFlag = 0;//是否已经绑定手机

        //已经实名认证的通过4.1.2用户信息返回的字段 realauthtype为YL，如果绑卡成功在绑卡查询接口中binding_id：1，0或者空表示没绑
        if("YL".equals(responseBO.getRealauthtype())){
//            familyDto.setRealNameFlag(1);
            realNameFlag = 1;
        }

        //转换为是否实名 调用4.1.20 绑卡认证信息查询接口
        MedicalCardQueryRequestBO medicalCardQueryRequestBO=new MedicalCardQueryRequestBO();
        medicalCardQueryRequestBO.setFunction_id(NeusoftFunctionId.QUERY_MEDICAL_CARD);
        hymedPaymentSettingDO=getNeusoftSessionId(requestVO);
        medicalCardQueryRequestBO.setSession_id(hymedPaymentSettingDO.getSessionId());
        medicalCardQueryRequestBO.setPrivateKey(hymedPaymentSettingDO.getPrivateKey());
        medicalCardQueryRequestBO.setPublicKey(hymedPaymentSettingDO.getPublicKey());
        medicalCardQueryRequestBO.setPortalLoginId(requestVO.getLoginName());

        MedicalCardQueryResponseBO queryResponseBO = neusoftService.doExecute(medicalCardQueryRequestBO, MedicalCardQueryResponseBO.class);

        if(queryResponseBO != null && queryResponseBO.getBizSuccess() && "1".equals(queryResponseBO.getBinding_id())){
            //如果绑卡成功在绑卡查询接口中binding_id：1，0或者空表示没绑
//            familyDto.setBindInsuranceCardFlag(1);
            bindInsuranceCardFlag = 1;
        }else{
//            familyDto.setBindInsuranceCardFlag(0);
            bindInsuranceCardFlag = 0;
        }
//        BaseService.getBaseClient().updatePersonalUserFamily(familyDto);

        //查询手机是否绑定 调用4.1.16	查询人员手机是否绑定接口
        UserMobileIsBindRequestBO isBindRequestBO = new UserMobileIsBindRequestBO();
        isBindRequestBO.setFunction_id(NeusoftFunctionId.IS_BIND_MOBILE_USER);
        isBindRequestBO.setSession_id(hymedPaymentSettingDO.getSessionId());
        isBindRequestBO.setPrivateKey(hymedPaymentSettingDO.getPrivateKey());
        isBindRequestBO.setPublicKey(hymedPaymentSettingDO.getPublicKey());
        isBindRequestBO.setUserid(responseBO.getUserid());//接口4.1.2中的 用户编号  userid
        UserMobileIsBindResponseBO isBindResponseBO = neusoftService.doExecute(isBindRequestBO, UserMobileIsBindResponseBO.class);

        //构造返回给前端的参数
        UserInfoResponseVO responseVO = new UserInfoResponseVO();
        if(isBindResponseBO != null && isBindResponseBO.getBizSuccess()){
            //bizSuccess=true,执行成功(未绑定) bizSuccess =false,执行失败（已经绑定）。
            bindMobileFlag = 0;
        }else{
            bindMobileFlag = 1;
        }
        responseVO.setPatName(responseBO.getName());
        responseVO.setLoginName(responseBO.getLoginid());
        responseVO.setRsPatId(responseBO.getUserid());
        responseVO.setPatIdType(responseBO.getIdType());
        responseVO.setPatIdNo(responseBO.getIdNmuber());
        responseVO.setPatSex(responseBO.getSex());
        responseVO.setPatMobile(responseBO.getMobilePhone());
        responseVO.setRealNameFlag(realNameFlag);
        responseVO.setBindInsuranceCardFlag(bindInsuranceCardFlag);
        responseVO.setBankAccount(queryResponseBO.getBank_account());
        responseVO.setBindMobileFlag(bindMobileFlag);
        return response.buildResultObject(responseVO);
    }

    /**
     * 一卡通用户登录
     * yyt.hymed.user.login
     * @param data
     * @param sessionId
     * @return
     */
    public Response loginUser(String data, String sessionId) {
        Response response = new Response();
        logger.info("一卡通用户登录 - 外部接口-->method:loginUser,data:" + data + "！");
        UserLoginrRequestVo requestVo = JsonUtils.parseObject(data, UserLoginrRequestVo.class);

        // 校验RequestVo的数据h
        try {
            Validator.getInstance().validate(requestVo);
        } catch (ValidateException ex) {
            logger.info(" 数据校验失败:" + ex.getMessage() + "|入参:" + data + "！");
            logger.error("error", ex);
            response.setResultCodeAndMessage("24001", "数据校验失败:" + ex.getMessage() + "！");
            return response;
        }
        //-----------------------------------------------------------------------------------------------------

//        PersonalUserFamilyDto familyDto = BaseService.getBaseClient().getPersonalUserFamily(requestVo.getFamilyId());
//        if(familyDto == null){
//            response.setResultCodeAndMessage("24002", "家人数据不存在！");
//            return response;
//        }

        //调用4.1.7	用户密码验证
        UserPassValidateRequestBO requestBO = new UserPassValidateRequestBO();
        requestBO.setFunction_id(NeusoftFunctionId.VALIDATE_USER_PASS);
        HymedPaymentSettingDO hymedPaymentSettingDO=getNeusoftSessionId(requestVo);
        requestBO.setSession_id(hymedPaymentSettingDO.getSessionId());
        requestBO.setPrivateKey(hymedPaymentSettingDO.getPrivateKey());
        requestBO.setPublicKey(hymedPaymentSettingDO.getPublicKey());
        requestBO.setPortalLoginId(requestVo.getLoginName());
        requestBO.setPassword(requestVo.getPassword());

        UserPassValidateResponseBO responseBO = neusoftService.doExecute(requestBO, UserPassValidateResponseBO.class);
        logger.info("neusoftService UserPassValidateResponseBO-->response="  + JsonUtils.toJsonString(responseBO));
        if (responseBO == null || !responseBO.getBizSuccess()) {
            String msg = (responseBO != null && StringUtils.isNotBlank(responseBO.getMessage())) ? responseBO.getMessage() : "登录失败";
            msg = msg.replaceAll("\"","'");
            response.setResultCodeAndMessage(ResponseEnum.Common.FAILURE.getCode(),msg);
            return response;
        }else{
            //调用用户信息获取，校验身份证是否一致
            //调用 4.1.2 用户信息获取
//            UserInfoRequestBO userInfoRequestBO = new UserInfoRequestBO();
//            userInfoRequestBO.setPortalLoginId(requestVo.getLoginName());
//            userInfoRequestBO.setFunction_id(NeusoftFunctionId.GET_USER_INFO);
//            hymedPaymentSettingDO=getNeusoftSessionId(requestVo);
//            userInfoRequestBO.setSession_id(hymedPaymentSettingDO.getSessionId());
//            userInfoRequestBO.setPrivateKey(hymedPaymentSettingDO.getPrivateKey());
//            userInfoRequestBO.setPublicKey(hymedPaymentSettingDO.getPublicKey());
//
//            UserInfoResponseBO userInfoResponseBO = neusoftService.doExecute(userInfoRequestBO, UserInfoResponseBO.class);
//            logger.info("neusoftService UserInfoResponseBO-->response="  + JsonUtils.toJsonString(responseBO));
//            if (userInfoResponseBO == null || !userInfoResponseBO.getBizSuccess()) {
//                response.setResultCodeAndMessage(ResponseEnum.Common.FAILURE.getCode(),(userInfoResponseBO != null && StringUtils.isNotBlank(userInfoResponseBO.getMessage())) ? userInfoResponseBO.getMessage() : "调用用户信息获取接口失败");
//                return response;
//            }
//            if(!familyDto.getIdCardNo().equals(userInfoResponseBO.getIdNmuber())){
//                response.setResultCodeAndMessage("24008","身份证不一致，不允许登录");
//                return response;
//            }

            response.setResultCodeAndMessage(ResponseEnum.Common.SUCCESS.getCode(),"登录成功");
            //更新family表
//            familyDto.setInsuranceLoginName(requestVo.getLoginName());
//            familyDto.setInsurancePassword(requestVo.getPassword());
//            BaseService.getBaseClient().updatePersonalUserFamily(familyDto);

            //登录账号保存到session中
            SessionInfo sessionInfo = CacheService.getSessionInfo(sessionId);
            if(sessionInfo == null) {
                String sessionid = PKGenerator.generateId();
                sessionInfo = new SessionInfo();
                sessionInfo.setSignKey(PKGenerator.generateId().substring(8, 24));
                sessionInfo.setSessionid(sessionid);
                sessionInfo.setInsuranceLoginName(requestVo.getLoginName());
                CacheService.setSessionInfo(sessionid, sessionInfo);
            }
        }

        return response;
    }

    /**
     * 一卡通用户注册
     * yyt.hymed.user.register
     * @param data
     * @param sessionId
     * @return
     */
    public Response registerUser(String data, String sessionId) {
        Response response = new Response();
        logger.info("一卡通用户注册 - 外部接口-->method:registerUser,data:" + data + "！");
        UserRegisterRequestVo requestVo = JsonUtils.parseObject(data, UserRegisterRequestVo.class);

        // 校验RequestVo的数据h
        try {
            Validator.getInstance().validate(requestVo);
        } catch (ValidateException ex) {
            logger.info(" 数据校验失败:" + ex.getMessage() + "|入参:" + data + "！");
            logger.error("error", ex);
            response.setResultCodeAndMessage("24001", "数据校验失败:" + ex.getMessage() + "！");
            return response;
        }
        //-----------------------------------------------------------------------------------------------------

//        PersonalUserFamilyDto familyDto = BaseService.getBaseClient().getPersonalUserFamily(requestVo.getFamilyId());
//        if(familyDto == null){
//            response.setResultCodeAndMessage("24002", "家人数据不存在！");
//            return response;
//        }

        UserRegisterRequestBO requestBO = new UserRegisterRequestBO();
        requestBO.setFunction_id(NeusoftFunctionId.REGISTER_USER);
        HymedPaymentSettingDO hymedPaymentSettingDO=getNeusoftSessionId(requestVo);
        requestBO.setSession_id(hymedPaymentSettingDO.getSessionId());
        requestBO.setPrivateKey(hymedPaymentSettingDO.getPrivateKey());
        requestBO.setPublicKey(hymedPaymentSettingDO.getPublicKey());

        requestBO.setPortalLoginId(requestVo.getLoginName());
        requestBO.setPassword(requestVo.getPassword());
        requestBO.setName(requestVo.getPatName());
        requestBO.setIdType("01");
        requestBO.setIdNumber(requestVo.getPatIdNo());
        requestBO.setNewPassword(requestVo.getPassword());
        requestBO.setDistrict("CHN");
        requestBO.setTel(requestVo.getPatMobile());
        requestBO.setLoginid("");
        requestBO.setSex(String.valueOf(getSex(requestVo.getPatIdNo())));
        requestBO.setEmail("");
        requestBO.setUserDerive("16");
        requestBO.setAab301("");
        requestBO.setIsValid("1");
        requestBO.setMer_id(hymedPaymentSettingDO.getMerchantId());

        UserRegisterResponseBO responseBO = neusoftService.doExecute(requestBO, UserRegisterResponseBO.class);
        logger.info("neusoftService UserRegisterResponseBO-->response="  + JsonUtils.toJsonString(responseBO));
        if (responseBO == null || !responseBO.getBizSuccess()) {
            response.setResultCodeAndMessage(ResponseEnum.Common.FAILURE.getCode(),(responseBO != null && StringUtils.isNotBlank(responseBO.getMessage())) ? responseBO.getMessage() : "注册失败");
        }else{
            response.setResultCodeAndMessage(ResponseEnum.Common.SUCCESS.getCode(),"注册成功");
            //更新family表
//            familyDto.setInsuranceLoginName(requestVo.getLoginName());
//            familyDto.setInsurancePassword(requestVo.getPassword());
//            BaseService.getBaseClient().updatePersonalUserFamily(familyDto);

            //注册成功后登录账号保存到session中
            SessionInfo sessionInfo = CacheService.getSessionInfo(sessionId);
            if(sessionInfo == null) {
                String sessionid = PKGenerator.generateId();
                sessionInfo = new SessionInfo();
                sessionInfo.setSignKey(PKGenerator.generateId().substring(8, 24));
                sessionInfo.setSessionid(sessionid);
                sessionInfo.setInsuranceLoginName(requestVo.getLoginName());
                CacheService.setSessionInfo(sessionid, sessionInfo);
            }
        }

        return response;
    }



    /**
     * 一卡通用户实名验证
     * yyt.hymed.user.real.name.validate
     * @param data
     * @param sessionId
     * @return
     */
    public Response validateUserRealName(String data, String sessionId) {
        Response response = new Response();
        logger.info("一卡通实名验证 --->method:validateUserRealName,data:" + data + "！");
        UserRealNameRequestVo requestVo = JsonUtils.parseObject(data, UserRealNameRequestVo.class);

        // 校验RequestVo的数据h
        try {
            Validator.getInstance().validate(requestVo);
        } catch (ValidateException ex) {
            logger.info(" 数据校验失败:" + ex.getMessage() + "|入参:" + data + "！");
            logger.error("error", ex);
            response.setResultCodeAndMessage("24001", "数据校验失败:" + ex.getMessage() + "！");
            return response;
        }
        //-----------------------------------------------------------------------------------------------------

//        PersonalUserFamilyDto familyDto = BaseService.getBaseClient().getPersonalUserFamily(requestVo.getFamilyId());
//        if(familyDto == null){
//            response.setResultCodeAndMessage("24002", "家人数据不存在！");
//            return response;
//        }
//        if(StringUtils.isBlank(familyDto.getInsuranceLoginName())){
//            response.setResultCodeAndMessage("24003", "请先登录或注册！");
//            return response;
//        }

        //调用 4.1.17	验证短信验证码接口
        SmsCodeValidateRequestBO smsRequestBO = new SmsCodeValidateRequestBO();
        smsRequestBO.setFunction_id(NeusoftFunctionId.VALIDATE_SMS_CODE);
        smsRequestBO.setPortalLoginId(requestVo.getLoginName());
        smsRequestBO.setMobile(requestVo.getPatMobile());
        smsRequestBO.setSmsCode(requestVo.getSmsCode());

        HymedPaymentSettingDO hymedPaymentSettingDO=getNeusoftSessionId(requestVo);
        smsRequestBO.setSession_id(hymedPaymentSettingDO.getSessionId());
        smsRequestBO.setPrivateKey(hymedPaymentSettingDO.getPrivateKey());
        smsRequestBO.setPublicKey(hymedPaymentSettingDO.getPublicKey());

        SmsCodeValidateResponseBO smsResponseBO = neusoftService.doExecute(smsRequestBO,SmsCodeValidateResponseBO.class);
        if (smsResponseBO == null || !smsResponseBO.getBizSuccess()) {
            response.setResultCodeAndMessage(ResponseEnum.Common.FAILURE.getCode(),
                    (smsResponseBO != null && StringUtils.isNotBlank(smsResponseBO.getMessage())) ? smsResponseBO.getMessage() : "验证码验证失败");
            return response;
        }


        UserRealNameValidateRequestBO requestBO = new UserRealNameValidateRequestBO();
        requestBO.setFunction_id(NeusoftFunctionId.VALIDATE_REAL_NAME);
        requestBO.setSession_id(hymedPaymentSettingDO.getSessionId());
        requestBO.setPrivateKey(hymedPaymentSettingDO.getPrivateKey());
        requestBO.setPublicKey(hymedPaymentSettingDO.getPublicKey());

        requestBO.setBankIdCard(requestVo.getBankCardNo());
        requestBO.setUsername(requestVo.getPatName());
        requestBO.setMobile(requestVo.getPatMobile());
        requestBO.setCertificateType(requestVo.getPatIdType());
        requestBO.setCertificateNum(requestVo.getPatIdNo());
//        requestBO.setPortalLoginId(familyDto.getInsuranceLoginName());
        requestBO.setPortalLoginId(requestVo.getLoginName());
        UserRealNameValidateResponseBO responseBO = neusoftService.doExecute(requestBO, UserRealNameValidateResponseBO.class);

        if (responseBO == null || !responseBO.getBizSuccess()) {
            response.setResultCodeAndMessage(ResponseEnum.Common.FAILURE.getCode(),
                    (responseBO != null && StringUtils.isNotBlank(responseBO.getMessage())) ? responseBO.getMessage() : "调用实名验证接口失败");
            return response;
        }else{
            //调用 4.1.18 用户认证信息修改
            UserRealNameConfirmRequestBO userRealNameConfirmRequestBO=new UserRealNameConfirmRequestBO();
            userRealNameConfirmRequestBO.setFunction_id(NeusoftFunctionId.CONFIRM_REAL_NAME);
             hymedPaymentSettingDO=getNeusoftSessionId(requestVo);
            userRealNameConfirmRequestBO.setSession_id(hymedPaymentSettingDO.getSessionId());
            userRealNameConfirmRequestBO.setPrivateKey(hymedPaymentSettingDO.getPrivateKey());
            userRealNameConfirmRequestBO.setPublicKey(hymedPaymentSettingDO.getPublicKey());
            userRealNameConfirmRequestBO.setUserid(requestVo.getRsPatId());
            userRealNameConfirmRequestBO.setRealAuthType(RealAuthTypeEnum.YL.getCode());
            userRealNameConfirmRequestBO.setPortalLoginId(requestVo.getLoginName());

            UserRealNameConfirmResponseBO confirmResponseBO = neusoftService.doExecute(userRealNameConfirmRequestBO, UserRealNameConfirmResponseBO.class);
            if (confirmResponseBO == null || !confirmResponseBO.getBizSuccess()) {
                response.setResultCodeAndMessage(ResponseEnum.Common.FAILURE.getCode(),
                        (confirmResponseBO != null && StringUtils.isNotBlank(confirmResponseBO.getMessage())) ? confirmResponseBO.getMessage() : "调用用户验证信息修改接口失败");
                return response;
            }else{
                //更新RealNameFlag标记
//                familyDto.setRealNameFlag(1);
//                BaseService.getBaseClient().updatePersonalUserFamily(familyDto);
                response.setResultCodeAndMessage("0","实名验证成功");
            }
        }

        return response;
    }




    /**
     * 一卡通用户余额查询
     * yyt.hymed.card.insurance.balance.get
     * @param data
     * @param sessionId
     * @return
     */
    public Response getCardInsuranceBalance(String data, String sessionId) {
        Response response = new Response();
        logger.info("一卡通用户余额查询 --->method:getCardInsuranceBalance,data:" + data + "！");
        UserBindCardRequestVo requestVO = JsonUtils.parseObject(data, UserBindCardRequestVo.class);

        // 校验RequestVo的数据h
        try {
            Validator.getInstance().validate(requestVO);
        } catch (ValidateException ex) {
            logger.info(" 数据校验失败:" + ex.getMessage() + "|入参:" + data + "！");
            logger.error("error", ex);
            response.setResultCodeAndMessage("24001", "数据校验失败:" + ex.getMessage() + "！");
            return response;
        }
        //-----------------------------------------------------------------------------------------------------

//        PersonalUserFamilyDto familyDto = BaseService.getBaseClient().getPersonalUserFamily(requestVO.getFamilyId());
//        if(familyDto == null){
//            response.setResultCodeAndMessage("24002", "家人数据不存在！");
//            return response;
//        }

        //调用4.1.20	绑卡认证信息查询接口
        MedicalCardQueryRequestBO medicalCardQueryRequestBO=new MedicalCardQueryRequestBO();
        medicalCardQueryRequestBO.setFunction_id(NeusoftFunctionId.QUERY_MEDICAL_CARD);
        HymedPaymentSettingDO hymedPaymentSettingDO=getNeusoftSessionId(requestVO);
        medicalCardQueryRequestBO.setSession_id(hymedPaymentSettingDO.getSessionId());
        medicalCardQueryRequestBO.setPrivateKey(hymedPaymentSettingDO.getPrivateKey());
        medicalCardQueryRequestBO.setPublicKey(hymedPaymentSettingDO.getPublicKey());
        medicalCardQueryRequestBO.setPortalLoginId(requestVO.getLoginName());

        MedicalCardQueryResponseBO queryResponseBO = neusoftService.doExecute(medicalCardQueryRequestBO, MedicalCardQueryResponseBO.class);


        if (queryResponseBO == null || !queryResponseBO.getBizSuccess()) {
            response.setResultCodeAndMessage(ResponseEnum.Common.FAILURE.getCode(),
                    (queryResponseBO != null && StringUtils.isNotBlank(queryResponseBO.getMessage())) ? queryResponseBO.getMessage() : "调用绑卡认证信息查询接口失败");
            return response;
        }else{
            //4.8.4	余额查询 // TODO: 2017/10/25
            
        }

        return response;
    }

    /**
     * 一卡通用户是否存在
     * yyt.hymed.user.exist
     * @param data
     * @param sessionId
     * @return
     */
    public Response existUser(String data, String sessionId) {
        Response response = new Response();
        logger.info("一卡通用户是否存在 --->method:getCardInsuranceBalance,data:" + data + "！");
        UserExistRequestVo requestVO = JsonUtils.parseObject(data, UserExistRequestVo.class);

        // 校验RequestVo的数据h
        try {
            Validator.getInstance().validate(requestVO);
        } catch (ValidateException ex) {
            logger.info(" 数据校验失败:" + ex.getMessage() + "|入参:" + data + "！");
            logger.error("error", ex);
            response.setResultCodeAndMessage("24001", "数据校验失败:" + ex.getMessage() + "！");
            return response;
        }
        //-----------------------------------------------------------------------------------------------------

        //调用4.1.3	用户是否存在验证
        UserExistRequestBO requestBO = new UserExistRequestBO();
        requestBO.setFunction_id(NeusoftFunctionId.EXIST_USER);
        HymedPaymentSettingDO hymedPaymentSettingDO=getNeusoftSessionId(requestVO);
        requestBO.setSession_id(hymedPaymentSettingDO.getSessionId());
        requestBO.setPrivateKey(hymedPaymentSettingDO.getPrivateKey());
        requestBO.setPublicKey(hymedPaymentSettingDO.getPublicKey());
        requestBO.setPortalLoginId(requestVO.getLoginName());

        UserExistResponseBO responseBO = neusoftService.doExecute(requestBO, UserExistResponseBO.class);
        logger.info("");
        if (responseBO == null || !responseBO.getBizSuccess()) {
            response.setResultCodeAndMessage(ResponseEnum.Common.FAILURE.getCode(),
                    (responseBO != null && StringUtils.isNotBlank(responseBO.getMessage())) ? responseBO.getMessage() : "调用用户是否存在验证接口失败");
            return response;
        }else{
            response.setResultCodeAndMessage("0",responseBO.getMessage());
        }

        return response;
    }

    /**
     * 一卡通用户信息修改接口
     * yyt.hymed.user.info.update
     * @param data
     * @param sessionId
     * @return
     */
    public Response updateUserInfo(String data, String sessionId) {
        Response response = new Response();
        logger.info("一卡通用户信息修改接口 --->method:updateUserInfo,data:" + data + "！");
        UserInfoUpdateRequestVo requestVO = JsonUtils.parseObject(data, UserInfoUpdateRequestVo.class);

        // 校验RequestVo的数据h
        try {
            Validator.getInstance().validate(requestVO);
        } catch (ValidateException ex) {
            logger.info(" 数据校验失败:" + ex.getMessage() + "|入参:" + data + "！");
            logger.error("error", ex);
            response.setResultCodeAndMessage("24001", "数据校验失败:" + ex.getMessage() + "！");
            return response;
        }
        //-----------------------------------------------------------------------------------------------------

        //调用4.1.4	用户信息修改
        UserInfoUpdateRequestBO requestBO = new UserInfoUpdateRequestBO();
        requestBO.setFunction_id(NeusoftFunctionId.UPDATE_USRE_INFO);
        HymedPaymentSettingDO hymedPaymentSettingDO=getNeusoftSessionId(requestVO);
        requestBO.setSession_id(hymedPaymentSettingDO.getSessionId());
        requestBO.setPrivateKey(hymedPaymentSettingDO.getPrivateKey());
        requestBO.setPublicKey(hymedPaymentSettingDO.getPublicKey());
        requestBO.setPortalLoginId(requestVO.getLoginName());
        requestBO.setName(requestVO.getPatName());
        requestBO.setSex(requestVO.getPatSex());

        UserInfoUpdateResponseBO responseBO = neusoftService.doExecute(requestBO, UserInfoUpdateResponseBO.class);
        if (responseBO == null || !responseBO.getBizSuccess()) {
            response.setResultCodeAndMessage(ResponseEnum.Common.FAILURE.getCode(),
                    (responseBO != null && StringUtils.isNotBlank(responseBO.getMessage())) ? responseBO.getMessage() : "调用用户信息修改接口失败");
            return response;
        }else{
            response.setResultCodeAndMessage("0",responseBO.getMessage());
        }

        return response;
    }

    /**
     * 3.2.9.	一卡通密码修改接口
     * yyt.hymed.user.password.update
     * @param data
     * @param sessionId
     * @return
     */
    public Response updateUserPassword(String data, String sessionId) {
        Response response = new Response();
        logger.info("一卡通密码修改接口 --->method:updateUserPassword,data:" + data + "！");
        UserPasswordUpdateRequestVo requestVO = JsonUtils.parseObject(data, UserPasswordUpdateRequestVo.class);

        // 校验RequestVo的数据h
        try {
            Validator.getInstance().validate(requestVO);
        } catch (ValidateException ex) {
            logger.info(" 数据校验失败:" + ex.getMessage() + "|入参:" + data + "！");
            logger.error("error", ex);
            response.setResultCodeAndMessage("24001", "数据校验失败:" + ex.getMessage() + "！");
            return response;
        }
        //-----------------------------------------------------------------------------------------------------

//        PersonalUserFamilyDto familyDto = BaseService.getBaseClient().getPersonalUserFamily(requestVO.getFamilyId());
//        if(familyDto == null){
//            response.setResultCodeAndMessage("24002", "家人数据不存在！");
//            return response;
//        }

        //调用4.1.5	用户密码修改
        UserPasswordUpdateRequestBO requestBO = new UserPasswordUpdateRequestBO();
        requestBO.setFunction_id(NeusoftFunctionId.UPDATE_USRE_PASSWORD);
        HymedPaymentSettingDO hymedPaymentSettingDO=getNeusoftSessionId(requestVO);
        requestBO.setSession_id(hymedPaymentSettingDO.getSessionId());
        requestBO.setPrivateKey(hymedPaymentSettingDO.getPrivateKey());
        requestBO.setPublicKey(hymedPaymentSettingDO.getPublicKey());
        requestBO.setPortalLoginId(requestVO.getLoginName());
        requestBO.setPassword(requestVO.getPassword());
        requestBO.setNewPassword(requestVO.getNewPassword());

        UserPasswordUpdateResponseBO responseBO = neusoftService.doExecute(requestBO, UserPasswordUpdateResponseBO.class);
        logger.info("neusoftService UserPasswordUpdateResponseBO-->response="  + JsonUtils.toJsonString(responseBO));
        if (responseBO == null || !responseBO.getBizSuccess()) {
            response.setResultCodeAndMessage(ResponseEnum.Common.FAILURE.getCode(),
                    (responseBO != null && StringUtils.isNotBlank(responseBO.getMessage())) ? responseBO.getMessage() : "调用用户密码修改接口失败");
            return response;
        }else{
            //登录名密码更新为空，引导用户重新登录
//            familyDto.setInsurancePassword("");
//            familyDto.setInsuranceLoginName("");
//            BaseService.getBaseClient().updatePersonalUserFamily(familyDto);
            PersonalUserFamilyDto familyDto = BaseService.getBaseClient().getPersonalUserFamilyByInsuranceName(requestVO.getLoginName());
            if(familyDto != null){
                familyDto.setInsurancePassword(requestVO.getNewPassword());
                BaseService.getBaseClient().updatePersonalUserFamily(familyDto);
            }
            response.setResultCodeAndMessage("0","密码修改成功");
        }

        return response;
    }

    /**
     * 3.2.11.	一卡通找回登录密码接口
     * yyt.hymed.user.login.password.find
     * @param data
     * @param sessionId
     * @return
     */
    public Response findUserLoginPassword(String data, String sessionId) {
        Response response = new Response();
        logger.info("一卡通找回登录密码接口 --->method:findUserLoginPassword,data:" + data + "！");
        UserLoginPasswordFindRequestVo requestVO = JsonUtils.parseObject(data, UserLoginPasswordFindRequestVo.class);

        // 校验RequestVo的数据h
        try {
            Validator.getInstance().validate(requestVO);
        } catch (ValidateException ex) {
            logger.info(" 数据校验失败:" + ex.getMessage() + "|入参:" + data + "！");
            logger.error("error", ex);
            response.setResultCodeAndMessage("24001", "数据校验失败:" + ex.getMessage() + "！");
            return response;
        }
        //-----------------------------------------------------------------------------------------------------

        //调用4.1.6	用户密码找回
        UserLoginPasswordFindRequestBO requestBO = new UserLoginPasswordFindRequestBO();
        requestBO.setFunction_id(NeusoftFunctionId.FIND_USER_LOGIN_PASSWORD);
        HymedPaymentSettingDO hymedPaymentSettingDO=getNeusoftSessionId(requestVO);
        requestBO.setSession_id(hymedPaymentSettingDO.getSessionId());
        requestBO.setPrivateKey(hymedPaymentSettingDO.getPrivateKey());
        requestBO.setPublicKey(hymedPaymentSettingDO.getPublicKey());
        requestBO.setPortalLoginId(requestVO.getLoginName());
        requestBO.setMobile(requestVO.getPatMobile());
        requestBO.setSmsCode(requestVO.getSmsCode());
        requestBO.setNewPassword(requestVO.getNewPassword());

        UserLoginPasswordFindResponseBO responseBO = neusoftService.doExecute(requestBO, UserLoginPasswordFindResponseBO.class);
        logger.info("neusoftService UserLoginPasswordFindResponseBO-->response="  + JsonUtils.toJsonString(responseBO));
        if (responseBO == null || !responseBO.getBizSuccess()) {
            response.setResultCodeAndMessage(ResponseEnum.Common.FAILURE.getCode(),
                    (responseBO != null && StringUtils.isNotBlank(responseBO.getMessage())) ? responseBO.getMessage() : "调用用户密码找回接口失败");
            return response;
        }else{
            //登录名密码更新为空，引导用户重新登录
//            familyDto.setInsurancePassword("");
//            familyDto.setInsuranceLoginName("");
//            BaseService.getBaseClient().updatePersonalUserFamily(familyDto);
            response.setResultCodeAndMessage("0","密码重置成功");
        }

        return response;
    }

    /**
     * 3.2.12.	一卡通找回账号接口
     * yyt.hymed.user.login.name.find
     * @param data
     * @param sessionId
     * @return
     */
    public Response findUserLoginName(String data, String sessionId) {
        Response response = new Response();
        logger.info("一卡通找回账号接口 --->method:findUserLoginName,data:" + data + "！");
        UserLoginNameFindRequestVo requestVO = JsonUtils.parseObject(data, UserLoginNameFindRequestVo.class);

        // 校验RequestVo的数据h
        try {
            Validator.getInstance().validate(requestVO);
        } catch (ValidateException ex) {
            logger.info(" 数据校验失败:" + ex.getMessage() + "|入参:" + data + "！");
            logger.error("error", ex);
            response.setResultCodeAndMessage("24001", "数据校验失败:" + ex.getMessage() + "！");
            return response;
        }
        //-----------------------------------------------------------------------------------------------------

        //调用4.1.21	用户账号找回接口
        UserLoginNameFindRequestBO requestBO = new UserLoginNameFindRequestBO();
        requestBO.setFunction_id(NeusoftFunctionId.FIND_USER_LOGIN_NAME);
        HymedPaymentSettingDO hymedPaymentSettingDO=getNeusoftSessionId(requestVO);
        requestBO.setSession_id(hymedPaymentSettingDO.getSessionId());
        requestBO.setPrivateKey(hymedPaymentSettingDO.getPrivateKey());
        requestBO.setPublicKey(hymedPaymentSettingDO.getPublicKey());
        requestBO.setName(requestVO.getUserName());
        requestBO.setDistrict("CHN");
        requestBO.setIdType("01");//requestVO.getPatIdType()
        requestBO.setIdNumber(requestVO.getPatIdNo());

        UserLoginNameFindResponseBO responseBO = neusoftService.doExecute(requestBO, UserLoginNameFindResponseBO.class);
        logger.info("neusoftService UserLoginNameFindResponseBO-->response="  + JsonUtils.toJsonString(responseBO));
        if (responseBO == null || !responseBO.getBizSuccess()) {
            response.setResultCodeAndMessage(ResponseEnum.Common.FAILURE.getCode(),
                    (responseBO != null && StringUtils.isNotBlank(responseBO.getMessage())) ? responseBO.getMessage() : "调用用户账号找回接口失败");
            return response;
        }else{
            UserLoginNameFindResponseVo responseVo = new UserLoginNameFindResponseVo();
            responseVo.setLoginName(responseBO.getLoginid());
            response.setResultCodeAndMessageAndResult("0","账号找回成功",JsonUtils.toJsonString(responseVo));
        }

        return response;
    }

    /**
     * 3.2.10.	卡通重置支付密码接口
     * yyt.hymed.user.pay.password.reset
     * @param data
     * @param sessionId
     * @return
     */
    public Response resetUserPayPassword(String data, String sessionId) {
        Response response = new Response();
        logger.info("一卡通重置支付密码接口 --->method:resetUserPayPassword,data:" + data + "！");
        UserPayPasswordResetRequestVo requestVO = JsonUtils.parseObject(data, UserPayPasswordResetRequestVo.class);

        // 校验RequestVo的数据h
        try {
            Validator.getInstance().validate(requestVO);
        } catch (ValidateException ex) {
            logger.info(" 数据校验失败:" + ex.getMessage() + "|入参:" + data + "！");
            logger.error("error", ex);
            response.setResultCodeAndMessage("24001", "数据校验失败:" + ex.getMessage() + "！");
            return response;
        }
        //-----------------------------------------------------------------------------------------------------


        //调用4.1.22  支付密码重置接口
        UserPayPasswordResetRequestBO requestBO = new UserPayPasswordResetRequestBO();
        requestBO.setFunction_id(NeusoftFunctionId.RESET_USER_PAY_PASSWORD);
        HymedPaymentSettingDO hymedPaymentSettingDO=getNeusoftSessionId(requestVO);
        requestBO.setSession_id(hymedPaymentSettingDO.getSessionId());
        requestBO.setPrivateKey(hymedPaymentSettingDO.getPrivateKey());
        requestBO.setPublicKey(hymedPaymentSettingDO.getPublicKey());

        requestBO.setIdNumber(requestVO.getPatIdNo());
        requestBO.setSmsCode(requestVO.getSmsCode());
        requestBO.setMobile(requestVO.getPatMobile());
        requestBO.setPortalLoginId(requestVO.getLoginName());
        requestBO.setBank_account(requestVO.getBankCardNo());
        requestBO.setNewPassword1(requestVO.getNewPayPassword());
        requestBO.setNewPassword2(requestVO.getNewPayPassword());

        UserPayPasswordResetResponseBO responseBO = neusoftService.doExecute(requestBO, UserPayPasswordResetResponseBO.class);
        logger.info("neusoftService UserPayPasswordResetResponseBO-->response="  + JsonUtils.toJsonString(responseBO));
        if (responseBO == null || !responseBO.getBizSuccess()) {
            response.setResultCodeAndMessage(ResponseEnum.Common.FAILURE.getCode(),
                    (responseBO != null && StringUtils.isNotBlank(responseBO.getMessage())) ? responseBO.getMessage() : "调用重置支付密码接口失败");
            return response;
        }else{
            response.setResultCodeAndMessage("0","支付密码重置成功");
        }

        return response;
    }

    /**
     * 3.2.14.	一卡通手机绑定接口
     * yyt.hymed.user.mobile.bind
     * @param data
     * @param sessionId
     * @return
     */
    public Response bindUserMobile(String data, String sessionId) {
        Response response = new Response();
        logger.info("一卡通手机绑定接口 --->method:bindUserMobile,data:" + data + "！");
        UserMobileBindRequestVO requestVO = JsonUtils.parseObject(data, UserMobileBindRequestVO.class);

        // 校验RequestVo的数据h
        try {
            Validator.getInstance().validate(requestVO);
        } catch (ValidateException ex) {
            logger.info(" 数据校验失败:" + ex.getMessage() + "|入参:" + data + "！");
            logger.error("error", ex);
            response.setResultCodeAndMessage("24001", "数据校验失败:" + ex.getMessage() + "！");
            return response;
        }
        //-----------------------------------------------------------------------------------------------------

        //调用 4.1.17	验证短信验证码接口
        SmsCodeValidateRequestBO smsRequestBO = new SmsCodeValidateRequestBO();
        smsRequestBO.setFunction_id(NeusoftFunctionId.VALIDATE_SMS_CODE);
        smsRequestBO.setPortalLoginId(requestVO.getLoginName());
        smsRequestBO.setMobile(requestVO.getPatMobile());
        smsRequestBO.setSmsCode(requestVO.getSmsCode());

        HymedPaymentSettingDO hymedPaymentSettingDO=getNeusoftSessionId(requestVO);
        smsRequestBO.setSession_id(hymedPaymentSettingDO.getSessionId());
        smsRequestBO.setPrivateKey(hymedPaymentSettingDO.getPrivateKey());
        smsRequestBO.setPublicKey(hymedPaymentSettingDO.getPublicKey());

        SmsCodeValidateResponseBO smsResponseBO = neusoftService.doExecute(smsRequestBO,SmsCodeValidateResponseBO.class);
        if (smsResponseBO == null || !smsResponseBO.getBizSuccess()) {
            response.setResultCodeAndMessage(ResponseEnum.Common.FAILURE.getCode(),
                    (smsResponseBO != null && StringUtils.isNotBlank(smsResponseBO.getMessage())) ? smsResponseBO.getMessage() : "验证码验证失败");
            return response;
        }

        if("1".equals(requestVO.getBindFlag())) {
            //调用4.1.14	手机绑定接口
            UserMobileBindRequestBO requestBO = new UserMobileBindRequestBO();
            requestBO.setFunction_id(NeusoftFunctionId.BIND_MOBILE_USER);
            requestBO.setBindType("mobile");
            requestBO.setEmail("");
            requestBO.setMobile(requestVO.getPatMobile());
            requestBO.setUserid(requestVO.getRsPatId());
            requestBO.setSession_id(hymedPaymentSettingDO.getSessionId());
            requestBO.setPrivateKey(hymedPaymentSettingDO.getPrivateKey());
            requestBO.setPublicKey(hymedPaymentSettingDO.getPublicKey());

            UserMobileBindResponseBO responseBO = neusoftService.doExecute(requestBO, UserMobileBindResponseBO.class);
            logger.info("neusoftService UserMobileBindResponseBO-->response=" + JsonUtils.toJsonString(responseBO));
            if (responseBO == null || !responseBO.getBizSuccess()) {
                response.setResultCodeAndMessage(ResponseEnum.Common.FAILURE.getCode(),
                        (responseBO != null && StringUtils.isNotBlank(responseBO.getMessage())) ? responseBO.getMessage() : "调用手机绑定接口失败");
                return response;
            } else {
                response.setResultCodeAndMessage("0", "手机绑定成功");
            }
        }else  if("2".equals(requestVO.getBindFlag())) {
            //调用4.1.15	修改手机绑定接口
            UserMobileBindUpdateRequestBO requestBO = new UserMobileBindUpdateRequestBO();
            requestBO.setFunction_id(NeusoftFunctionId.UPDATE_BIND_MOBILE_USER);
            requestBO.setMobile(requestVO.getPatMobile());
            requestBO.setUserid(requestVO.getRsPatId());
            requestBO.setSession_id(hymedPaymentSettingDO.getSessionId());
            requestBO.setPrivateKey(hymedPaymentSettingDO.getPrivateKey());
            requestBO.setPublicKey(hymedPaymentSettingDO.getPublicKey());

            UserMobileBindUpdateResponseBO responseBO = neusoftService.doExecute(requestBO, UserMobileBindUpdateResponseBO.class);
            logger.info("neusoftService UserMobileBindUpdateResponseBO-->response=" + JsonUtils.toJsonString(responseBO));
            if (responseBO == null || !responseBO.getBizSuccess()) {
                response.setResultCodeAndMessage(ResponseEnum.Common.FAILURE.getCode(),
                        (responseBO != null && StringUtils.isNotBlank(responseBO.getMessage())) ? responseBO.getMessage() : "调用修改手机绑定接口失败");
                return response;
            } else {
                response.setResultCodeAndMessage("0", "修改手机绑定成功");
            }
        }

        return response;
    }


    /**
     * 3.2.15.	一卡通通过账号获取用户信息接口
     * yyt.hymed.user.info.by.loginname.get
     * @param params
     * @return
     */
    public ResponseObject getUserInfoByLoginname(String params) {
        ResponseObject response = new ResponseObject();
        int realNameFlag=0;
        int bindInsuranceCardFlag=0;

        UserInfoRequestVO requestVO = JsonUtils.parseObject(params, UserInfoRequestVO.class);
        assert requestVO != null;

        //补全信息
        requestVO.setAppCode("yyt48fe49dbe6eb1ceh");
        requestVO.setOrgCode("hysrmyy");
        requestVO.setPayCode("HYMEDUNION");

        //调用 4.1.2 用户信息获取
        UserInfoRequestBO requestBO = new UserInfoRequestBO();
        requestBO.setPortalLoginId(requestVO.getLoginName());
        requestBO.setFunction_id(NeusoftFunctionId.GET_USER_INFO);
        HymedPaymentSettingDO hymedPaymentSettingDO=getNeusoftSessionId(requestVO);
        requestBO.setSession_id(hymedPaymentSettingDO.getSessionId());
        requestBO.setPrivateKey(hymedPaymentSettingDO.getPrivateKey());
        requestBO.setPublicKey(hymedPaymentSettingDO.getPublicKey());

        UserInfoResponseBO responseBO = neusoftService.doExecute(requestBO, UserInfoResponseBO.class);

        if (responseBO == null || !responseBO.getBizSuccess()) {
            return response.buildResultCodeAndMessageAndResult(
                    ResponseEnum.Common.FAILURE.getCode(),
                    ResponseEnum.Common.FAILURE.getMessage(),
                    responseBO);
        }

        //已经实名认证的通过4.1.2用户信息返回的字段 realauthtype为YL，如果绑卡成功在绑卡查询接口中binding_id：1，0或者空表示没绑
        if("YL".equals(responseBO.getRealauthtype())){
            realNameFlag=1;
        }

        //转换为是否实名 调用4.1.20 绑卡认证信息查询接口
        MedicalCardQueryRequestBO medicalCardQueryRequestBO=new MedicalCardQueryRequestBO();
        medicalCardQueryRequestBO.setFunction_id(NeusoftFunctionId.QUERY_MEDICAL_CARD);
        hymedPaymentSettingDO=getNeusoftSessionId(requestVO);
        medicalCardQueryRequestBO.setSession_id(hymedPaymentSettingDO.getSessionId());
        medicalCardQueryRequestBO.setPrivateKey(hymedPaymentSettingDO.getPrivateKey());
        medicalCardQueryRequestBO.setPublicKey(hymedPaymentSettingDO.getPublicKey());
        medicalCardQueryRequestBO.setPortalLoginId(requestVO.getLoginName());

        MedicalCardQueryResponseBO queryResponseBO = neusoftService.doExecute(medicalCardQueryRequestBO, MedicalCardQueryResponseBO.class);
        if(queryResponseBO != null && queryResponseBO.getBizSuccess() && "1".equals(queryResponseBO.getBinding_id())){
            //如果绑卡成功在绑卡查询接口中binding_id：1，0或者空表示没绑
            bindInsuranceCardFlag=1;
        }else{
            bindInsuranceCardFlag=0;
        }

        //查询手机是否绑定 调用4.1.16	查询人员手机是否绑定接口
        UserMobileIsBindRequestBO isBindRequestBO = new UserMobileIsBindRequestBO();
        isBindRequestBO.setFunction_id(NeusoftFunctionId.IS_BIND_MOBILE_USER);
        isBindRequestBO.setSession_id(hymedPaymentSettingDO.getSessionId());
        isBindRequestBO.setPrivateKey(hymedPaymentSettingDO.getPrivateKey());
        isBindRequestBO.setPublicKey(hymedPaymentSettingDO.getPublicKey());
        isBindRequestBO.setUserid(responseBO.getUserid());//接口4.1.2中的 用户编号  userid
        UserMobileIsBindResponseBO isBindResponseBO = neusoftService.doExecute(isBindRequestBO, UserMobileIsBindResponseBO.class);

        //构造返回给前端的参数
        UserInfoResponseVO responseVO = new UserInfoResponseVO();
        if(isBindResponseBO != null && isBindResponseBO.getBizSuccess()){
            //bizSuccess=true,执行成功(未绑定) bizSuccess =false,执行失败（已经绑定）。
            responseVO.setBindMobileFlag(0);
        }else{
            responseVO.setBindMobileFlag(1);
        }
        responseVO.setPatName(responseBO.getName());
        responseVO.setLoginName(responseBO.getLoginid());
        responseVO.setRsPatId(responseBO.getUserid());
        responseVO.setPatIdType(responseBO.getIdType());
        responseVO.setPatIdNo(responseBO.getIdNmuber());
        if(!StringUtils.isBlank(responseBO.getIdNmuber())&&responseBO.getIdNmuber().length()>14){
            responseVO.setPatSex(String.valueOf(IdCardUtils.getSexFlagByIdCard(responseBO.getIdNmuber())));
        }else{
            responseVO.setPatSex("3");
        }
        responseVO.setPatMobile(responseBO.getMobilePhone());
        responseVO.setRealNameFlag(realNameFlag);
        responseVO.setBindInsuranceCardFlag(bindInsuranceCardFlag);
        responseVO.setBankAccount(queryResponseBO.getBank_account());
        return response.buildResultObject(responseVO);
    }



    /**
     * 获取用户已登录的账号
     * yyt.hymed.user.insurance.login.name.get
     * @return
     */
    public ResponseObject getUserInsuranceLoginName(String data, String sessionId) {
        ResponseObject response = new ResponseObject ();
        SessionInfo sessionInfo = CacheService.getSessionInfo(sessionId);
        if(sessionInfo == null || StringUtils.isBlank(sessionInfo.getInsuranceLoginName())){
            response.setResultCodeAndMessage("-1","用户未登录或已超时，请先登录");
            return response;
        }

        Map<String,String> map = new HashMap();
        map.put("insurance_login_name",sessionInfo.getInsuranceLoginName());
        response.buildResultObject(map);
        return response;

    }


    /**
     * 就诊人绑定社保卡账号
     * yyt.hymed.user.family.bind
     * @param data
     * @return
     */
    public ResponseObject bindUserFamily(String data) {
        ResponseObject response = new ResponseObject();

        UserLoginrRequestVo requestVo = JsonUtils.parseObject(data, UserLoginrRequestVo.class);

        // 校验RequestVo的数据
        try {
            Validator.getInstance().validate(requestVo);
        } catch (ValidateException ex) {
            logger.info(" 数据校验失败:" + ex.getMessage() + "|入参:" + data + "！");
            logger.error("error", ex);
            response.setResultCodeAndMessage("24001", "数据校验失败:" + ex.getMessage() + "！");
            return response;
        }
        //-----------------------------------------------------------------------------------------------------

        PersonalUserFamilyDto familyDto = BaseService.getBaseClient().getPersonalUserFamily(requestVo.getFamilyId());
        if(familyDto == null){
            response.setResultCodeAndMessage("24002", "家人数据不存在！");
            return response;
        }

        //调用4.1.7	用户密码验证
        UserPassValidateRequestBO requestBO = new UserPassValidateRequestBO();
        requestBO.setFunction_id(NeusoftFunctionId.VALIDATE_USER_PASS);
        HymedPaymentSettingDO hymedPaymentSettingDO=getNeusoftSessionId(requestVo);
        requestBO.setSession_id(hymedPaymentSettingDO.getSessionId());
        requestBO.setPrivateKey(hymedPaymentSettingDO.getPrivateKey());
        requestBO.setPublicKey(hymedPaymentSettingDO.getPublicKey());
        requestBO.setPortalLoginId(requestVo.getLoginName());
        requestBO.setPassword(requestVo.getPassword());

        UserPassValidateResponseBO responseBO = neusoftService.doExecute(requestBO, UserPassValidateResponseBO.class);
        logger.info("neusoftService UserPassValidateResponseBO-->response="  + JsonUtils.toJsonString(responseBO));
        if (responseBO == null || !responseBO.getBizSuccess()) {
            String msg = (responseBO != null && StringUtils.isNotBlank(responseBO.getMessage())) ? responseBO.getMessage() : "账号或密码不正确";
            msg = msg.replaceAll("\"","'");
            response.setResultCodeAndMessage(ResponseEnum.Common.FAILURE.getCode(),msg);
            return response;
        }else{
            //调用用户信息获取，校验身份证是否一致
            //调用 4.1.2 用户信息获取
            UserInfoRequestBO userInfoRequestBO = new UserInfoRequestBO();
            userInfoRequestBO.setPortalLoginId(requestVo.getLoginName());
            userInfoRequestBO.setFunction_id(NeusoftFunctionId.GET_USER_INFO);
            hymedPaymentSettingDO=getNeusoftSessionId(requestVo);
            userInfoRequestBO.setSession_id(hymedPaymentSettingDO.getSessionId());
            userInfoRequestBO.setPrivateKey(hymedPaymentSettingDO.getPrivateKey());
            userInfoRequestBO.setPublicKey(hymedPaymentSettingDO.getPublicKey());

            UserInfoResponseBO userInfoResponseBO = neusoftService.doExecute(userInfoRequestBO, UserInfoResponseBO.class);
            logger.info("neusoftService UserInfoResponseBO-->response="  + JsonUtils.toJsonString(responseBO));
            if (userInfoResponseBO == null || !userInfoResponseBO.getBizSuccess()) {
                response.setResultCodeAndMessage(ResponseEnum.Common.FAILURE.getCode(),(userInfoResponseBO != null && StringUtils.isNotBlank(userInfoResponseBO.getMessage())) ? userInfoResponseBO.getMessage() : "调用用户信息获取接口失败");
                return response;
            }
            if(!familyDto.getIdCardNo().equals(userInfoResponseBO.getIdNmuber())){
                response.setResultCodeAndMessage("24008","身份证不一致，不允许绑定");
                return response;
            }

            //更新family表
            familyDto.setInsuranceLoginName(requestVo.getLoginName());
            familyDto.setInsurancePassword(requestVo.getPassword());
            BaseService.getBaseClient().updatePersonalUserFamily(familyDto);
            response.setResultCodeAndMessage(ResponseEnum.Common.SUCCESS.getCode(),"绑定成功");
        }
        return response;
    }

    /**
     * 根据身份证获取性别
     * @param value
     * @return
     */
    public static int getSex(String value) {
        if (StringUtils.isNotBlank(value) && value.length() == 18) {
            return Integer.valueOf(value.substring(16, 17)) % 2 == 0 ? 2 : 1;
        } else {
            return 0;
        }
    }
}
